<template>
  <div class="login-container">
    <div class="login-content">
      <el-card class="login-card">
        <div class="login-header">
          <img src="/logo.svg" alt="logo" class="logo" />
          <h2 class="title">鱼皮接口</h2>
          <p class="subtitle">API 开放平台</p>
        </div>

        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          @submit.prevent="handleSubmit"
        >
          <el-form-item prop="userAccount">
            <el-input
              v-model="loginForm.userAccount"
              placeholder="用户名: admin or user"
              size="large"
            />
          </el-form-item>
          
          <el-form-item prop="userPassword">
            <el-input
              v-model="loginForm.userPassword"
              type="password"
              placeholder="密码: ant.design"
              size="large"
              show-password
            />
          </el-form-item>

          <div class="login-options">
            <el-checkbox v-model="loginForm.autoLogin">自动登录</el-checkbox>
            <a href="#" class="forgot-password">忘记密码 ?</a>
          </div>

          <el-button
            type="primary"
            size="large"
            class="login-btn"
            :loading="loading"
            @click="handleSubmit"
          >
            登录
          </el-button>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()

const loginFormRef = ref()
const loading = ref(false)

const loginForm = reactive({
  userAccount: '',
  userPassword: '',
  autoLogin: true
})

const loginRules = {
  userAccount: [
    { required: true, message: '用户名是必填项！', trigger: 'blur' }
  ],
  userPassword: [
    { required: true, message: '密码是必填项！', trigger: 'blur' }
  ]
}

const handleSubmit = async () => {
  if (!loginFormRef.value) return
  
  try {
    await loginFormRef.value.validate()
    loading.value = true
    
    const loginData = { 
      userAccount: loginForm.userAccount, 
      userPassword: loginForm.userPassword 
    }
    
    const result = await userStore.login(loginData)
    
    if (result.success) {
      ElMessage.success('登录成功')
      const redirect = new URLSearchParams(window.location.search).get('redirect') || '/'
      router.push(redirect)
    } else {
      ElMessage.error(result.message || '登录失败')
    }
  } catch (error) {
    console.error('登录错误:', error)
    ElMessage.error('登录失败，请检查用户名和密码')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-content {
  width: 100%;
  max-width: 400px;
}

.login-card {
  border-radius: 8px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header .logo {
  width: 64px;
  height: 64px;
  margin-bottom: 16px;
}

.login-header .title {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin: 0 0 8px 0;
}

.login-header .subtitle {
  font-size: 14px;
  color: #666;
  margin: 0;
}

.login-form .login-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 24px 0;
}

.login-form .forgot-password {
  color: #409eff;
  text-decoration: none;
}

.login-form .forgot-password:hover {
  text-decoration: underline;
}

.login-form .login-btn {
  width: 100%;
  margin-bottom: 24px;
}
</style>
